# This method is for least square method
# parsedata.R must be run firstly to generate train dataset
load("train.data.normal.rda")
linear.model = lm(lpsa~lcavol+lweight+age+lbph+svi+lcp+gleason+pgg45,data=train.data.normal)
summary(linear.model)
# 结果与书P50页表3.2一致

# 计算检验误差和标准误差(Test Error and Std Error)
load("test.data.normal.rda")
a<-rep(1,length(test.data.normal[,1]))
X.test<-cbind(Intercept=a,test.data.normal[,-length(test.data.normal)])
Y.test<-as.matrix(X.test)%*%as.matrix(linear.model$coefficients)
Y.true<-as.matrix(test.data.normal[,length(test.data.normal)])
Test.Error<-sum((Y.test-Y.true)*(Y.test-Y.true))/length(Y.test)
#Std.Error<-sum((Y.test-Y.true)*(Y.test-Y.true))/(length(Y.test)-10)/sqrt(length(Y.test)-11)
# 结果与书P63页表3.3一致,标准误差计算错误,不过不影响理解
